package com.alks.function.service.impl.biocontrol.deptwork;

import com.alks.common.utils.beanutils.BeanUtil;
import com.alks.entity.data.entity.PcLtNew;
import com.alks.entity.data.enums.biocontrol.TimeCaluEnum;
import com.alks.entity.data.response.ResponseInfo;
import com.alks.function.data.request.biocontrol.deptwork.TimeRequest;
import com.alks.function.mapper.biocontrol.deptwork.BC_DW_PcLtNewMapper;
import com.alks.function.service.biocontrol.deptwork.ITimeDefService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.alks.entity.data.enums.ResultCodeEnum.FRONT_IN_EMPTY;

@Service
@Slf4j
@RequiredArgsConstructor
public class TimeDefServiceImpl extends ServiceImpl<BC_DW_PcLtNewMapper, PcLtNew> implements ITimeDefService {
    private final BC_DW_PcLtNewMapper mapper;

    @Override
    public ResponseInfo getTimeQuery() {
        /*获取数据*/
        List<PcLtNew> lt = lambdaQuery().eq(PcLtNew::getLtType, 1).list()
                .stream().map(a->{
                    a.setCaluType(TimeCaluEnum.getType(a.getCaluType()));       //处理计算方法
                    return a;
                }).collect(Collectors.toList());
        List<PcLtNew> rt = lambdaQuery().eq(PcLtNew::getLtType, 2).list()
                .stream().map(a->{
                    a.setCaluType(TimeCaluEnum.getType(a.getCaluType()));
                    return a;
                }).collect(Collectors.toList());
        Map<String,Object> map=new HashMap<>();
        map.put("lt",lt);
        map.put("rt",rt);
        return ResponseInfo.ok(map);
    }

    @Override
    public ResponseInfo timeUpdate(TimeRequest request) {
        /*根据ID更新数据*/
        lambdaUpdate().eq(PcLtNew::getId,request.getId())
                .set(PcLtNew::getSeqNo,request.getSeqNo())
                .set(PcLtNew::getStepCode,request.getStepCode())
                .set(PcLtNew::getStepName,request.getStepName())
                .set(PcLtNew::getLtDays,request.getLtDays())
                .set(PcLtNew::getCaluType,TimeCaluEnum.getCode(request.getCaluType()))      //修改类型为代码
                .set(PcLtNew::getRemark,request.getRemark()).update();
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo timeDelete(TimeRequest request) {
        /*根据ID删除*/
        mapper.deleteById(request.getId());
        return ResponseInfo.ok();
    }

    @Override
    public ResponseInfo timeAdd(TimeRequest request) {
        /*判断数据健壮性*/
        if (request.getLtType()==null||request.getLtType().isEmpty()){
            log.error("请求方法：timeAdd\n错误原因：没有传入指定的参数");
            return ResponseInfo.error("错误",FRONT_IN_EMPTY.getCode());
        }
        /*准备要写入的数据*/
        PcLtNew pcLtNew = BeanUtil.copyBean(request, PcLtNew.class);
        pcLtNew.setCaluType(TimeCaluEnum.getCode(request.getCaluType()));
        pcLtNew.setId(null);
        pcLtNew.setCompanyId("BF");
        /*写入数据*/
        mapper.insert(pcLtNew);
        return ResponseInfo.ok();
    }
}
